package org.jeecg.common.util;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class RecursiveFileDeleter {

    /**
     * 递归删除给定路径下的所有文件和子目录。
     *
     * @param path 要删除的目录或文件的路径
     */
    public static void deleteRecursively(Path path) {
        try {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            // Handle the exception as appropriate for your application.
            System.err.println("Failed to delete: " + path + ". Reason: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        // Example usage:
        Path dirToDelete = Paths.get("/path/to/directory");
        deleteRecursively(dirToDelete);
    }
}